/* 
 * StikiWeb Wiki - a Wiki for Hierarchical Organizations
 * $Id: StikiAction.java,v 1.3 2004/05/05 05:23:43 biglee Exp $
 * 
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the StikiWeb Wiki 
 *
 * The Initial Developer of the Original Code is Lee Haslup (biglee@haslups.com).
 * Portions created by the Initial Developer are Copyright (C) 2004
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** 
 */
package org.stikiweb.struts;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;

import org.stikiweb.contentMagnager.PermissionManager;
import org.stikiweb.contentMagnager.StikiWebContext;

/**
 * @author Allen L (BigLee) Haslup
 *
 */
public abstract class StikiAction extends Action {
	
	protected StikiWebContext wikiContext;
	protected void setup(HttpServletRequest req) {
		wikiContext = StikiWebContext.solo(req);
	}
	
	protected String getUserName(HttpServletRequest req) {
		return PermissionManager.solo().getUserName(req);
	}
	
	protected String userIdentityString(HttpServletRequest req) {
		String retval = getUserName(req);
		if("Guest".equalsIgnoreCase(retval)) {
			String guestName = PermissionManager.solo().getGuestName(req);
			if(guestName.length()>0) {
				retval = "A Guest identified as "+guestName;
			}
		}
		return retval;
	}
	
	protected boolean isLoggedIn(HttpServletRequest req) {
		return !getUserName(req).equals("Visitor");
	}
	
	protected ActionForward goLoginFirst(String pendingAction, String pendingOp, String pendingPage, HttpServletRequest req) {
		HttpSession session = req.getSession();
		session.setAttribute("pendingAction",pendingAction);
		session.setAttribute("pendingOp",pendingOp);
		session.setAttribute("pendingPage",pendingPage);
		return new ActionForward("/WEB-INF/login.jsp");
	}
}
